package com.cx.healthex.client.admin.dao.act;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.cx.base.apiModel.PageModel;
import com.cx.base.autoDoc.annotations.AutoDocField;
import com.cx.base.dao.AbstractBaseDao;
import com.cx.base.utils.CommonUtils;
import com.cx.base.utils.LogInfoUtil;
import com.cx.healthex.client.admin.dto.act.ActTemplateDTO;
import com.cx.healthex.client.admin.dto.act.ActivityDTO;

@Repository
public class ActivityDAO extends AbstractBaseDao{
	
	/**
	 * 查询所有活动模板
	 * */
	public PageModel queryActTemplates(Integer pageNum, Integer itemCount){
		
		String sql = " select * from iat_activity_template ";
		List<Object> parameter = new ArrayList<Object>();
		
		PageModel<ActTemplateDTO> pageModel = getPageList(pageNum, itemCount, sql, parameter.toArray(), ParameterizedBeanPropertyRowMapper.newInstance(ActTemplateDTO.class));
		
		return pageModel;
	}
	
	
	/**
	 * 保存或更新活动
	 * */
	public ActivityDTO saveOrUpdateActivity(ActivityDTO activityDTO){
		try {
			if(activityDTO != null ){
				if(activityDTO.getActId() > 0){
					update(activityDTO, "iat_activity", "act_id", true);
				}else{
					//生成唯一码
					String actCode = CommonUtils.getPrimaryKey();
					activityDTO.setActCode(actCode);
					insert(activityDTO, "iat_activity", "act_id", true);
					
					//查询活动信息
					String sql = " select * from iat_activity where act_code = ? ";
					List<Object> parameter = new ArrayList<Object>();
					parameter.add(actCode);
					PageModel<ActivityDTO> pageModel = getPageList(1, 1, sql, parameter.toArray(), ParameterizedBeanPropertyRowMapper.newInstance(ActivityDTO.class));
					if(pageModel != null && pageModel.getItems().size() > 0){
						return pageModel.getItems().get(0);
					}
				}
			}
		} catch (IllegalArgumentException e) {
			LogInfoUtil.error(this.getClass(), "saveOrUpdateActivity", "出错--》》传参："+activityDTO.toString(), e);
		} catch (IllegalAccessException e) {
			LogInfoUtil.error(this.getClass(), "saveOrUpdateActivity", "出错--》》传参："+activityDTO.toString(), e);
		}

		return activityDTO;
	}
	
	
	
	/**
	 * 根据活动ID查询活动
	 * */
	public ActivityDTO queryActivityByActId(Integer actId){
		
		String sql = " select * from iat_activity where act_id = ? ";
		List<Object> parameter = new ArrayList<Object>();
		parameter.add(actId);
		PageModel<ActivityDTO> pageModel = getPageList(1, 1, sql, parameter.toArray(), ParameterizedBeanPropertyRowMapper.newInstance(ActivityDTO.class));
		if(pageModel != null && pageModel.getItems().size() > 0){
			return pageModel.getItems().get(0);
		}
		return null;
	}
	
	/**
	 * 根据活动ID查询活动
	 * */
	public PageModel queryActivity(Integer pageNum, Integer itemCount,Integer consumerId){	
		String sql = " select * from iat_activity where consumer_id = ? ";
		List<Object> parameter = new ArrayList<Object>();
		parameter.add(consumerId);
		PageModel<ActivityDTO> pageModel = getPageList(pageNum, itemCount, sql, parameter.toArray(), ParameterizedBeanPropertyRowMapper.newInstance(ActivityDTO.class));
		return pageModel;
	}
	
	

}
